Imports System.Diagnostics
Imports System.IO
Imports C3I.CodegenTools


Module basMainApp

    Sub Main()
        Dim tgenerateParameters As GenerateParameters, parametersFile As String
        Dim argumentIndex As Integer, argument As String
        Dim layoutConfig As New ConfigSettings
        Dim retObject As Object
        Dim codeGeneration As New GenerateCode


        If My.Application.CommandLineArgs.Count = 0 Then
            Console.WriteLine("YACGENCL - Yet another code generator command line ")
            Console.WriteLine("yacgencl gprfile.gpr (key1 value1, key2, value2 , keyn, valuen)")
            End
        End If

        'For Each argument In My.Application.CommandLineArgs
        'Console.WriteLine(argument)
        'Next

        'get the parameter name
        parametersFile = My.Application.CommandLineArgs(0)

        'attempt to load default settings 
        If My.Computer.FileSystem.FileExists(parametersFile) Then
            tgenerateParameters = Utils.LoadSerializedFile(parametersFile)
        Else
            Console.WriteLine("Can't find property file %1 . Exiting", parametersFile)
            End
        End If

        'attempt to load default settings 
        If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath & "\default.dbs") Then
            retObject = Utils.LoadSerializedFile(My.Application.Info.DirectoryPath & "\default.dbs")
            layoutConfig = retObject
        Else
            Console.WriteLine("Can't find file default.dbs in current directory. Exiting")
            End
        End If

        'get function code
        codeGeneration.ScriptLogic = layoutConfig.PairValues("FunctionCode")(1)

        codeGeneration.GenerateParameters = tgenerateParameters

        'check for command line arguments
        If My.Application.CommandLineArgs.Count > 0 Then
            For argumentIndex = 1 To My.Application.CommandLineArgs.Count - 1 Step 2
                argument = My.Application.CommandLineArgs(argumentIndex)

                Console.Write(argument & ":  " & My.Application.CommandLineArgs(argumentIndex + 1) & vbCrLf)
                codeGeneration.ModifyProperty(argument, My.Application.CommandLineArgs(argumentIndex + 1))
            Next
        End If

        Try
            codeGeneration.LoadTemplate()
            codeGeneration.LoadDatabaseStructure()
            codeGeneration.GenerateOutput()
        Catch cgex As CodeGenException
            Console.WriteLine("****************Error************")
            Console.WriteLine(cgex.Message)
            Console.WriteLine("Source " & cgex.InnerException.Source)
            Console.WriteLine("Source " & cgex.Source)

        Catch ex As Exception
            Console.WriteLine("****************Error************")
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.Source)
        End Try

    End Sub

End Module
